package com.ray.server;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketAddress;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Queue;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import com.ray.basic.BASIC;
import com.ray.server.Server_out;

public class RW_myQ {
	private ReadWriteLock rwl = new ReentrantReadWriteLock();
	
	public String[] reveive_analyse(int clientid, String receive) throws SQLException{
		Server_in object_in= TCPEchoServer.get_map_read().get(clientid);
		String[] parts= receive.split("&&");
		String[] info_array= new String[2];
		if (parts[0].equals("$$LOGIN")){
			String regName= parts[1];
			String regPassword=parts[2];
			String sql="select*from chatreg where username='"+regName+"' and password='"+regPassword+"'"; 
			java.sql.ResultSet rs= TCPEchoServer.get_mylog().executeQuery(sql);
			if (!rs.next()){
				info_array=ErrorReturn( object_in, clientid);
			}
			else{
				object_in.set_accept(true);
				System.out.println("RW_myQ::register successfully");
				String username= regName;
				object_in.set_username(username);
				OutputStream out= object_in.get_out();
				TCPEchoServer.set_map_out(username, out);
				Socket clntSock= object_in.get_clntSock();
				TCPEchoServer.set_map_socket(username, clntSock);
				info_array[0]=username;
				info_array[1]="$$ID&&"+username;
			}
		}
		else if (parts[0].equals("$$REQUEST") && object_in.get_accept()) {
			StringBuilder builder = new StringBuilder();
			ArrayList<String> userlist=BASIC.get_active_client();
			for(String s : userlist) builder.append(s);
			info_array[0]= object_in.get_username();
			info_array[1]= builder.toString();
		}
		else if (parts[0].equals("$$END")&& object_in.get_accept()) {
			info_array[0]=object_in.get_username();
			info_array[1]="$$MSSG&&NULL&&END_FROM_CLIENT";
		}
		else if (parts[0].equals("$$MSSG")&& object_in.get_accept()){
			String forwardid= parts[1];
			System.out.println("RW_myQ::forwardid"+forwardid);
			if (BASIC.get_active_client().contains("&&"+forwardid)) {
				info_array[0]= forwardid;
				info_array[1]= receive;
			}
			else {
				info_array[0]= object_in.get_username();
				info_array[1]= "$$MSSG&&NULL&&This guy is offline";
			}
		}
		else if (parts[0].equals("$$VIDEO")&& object_in.get_accept()){
			String video_id=parts[1];
			info_array[0]= object_in.get_username();
			SocketAddress remote= Udp_in.get_map_remote().get(video_id);
			info_array[1]="$$VIDEO"+"&&"+remote.toString();
		}
		else if (parts[0].equals("$$FILE")&& object_in.get_accept()){
			String file_id=parts[1];
			info_array[0]= object_in.get_username();
			SocketAddress remote= Udp_in.get_map_remote().get(file_id);
			info_array[1]="$$FILE"+"&&"+remote.toString();
		}
		else if (!object_in.get_accept()){
			info_array=ErrorReturn( object_in, clientid);
		}
		else System.out.println("ERROR::rw_myQ::"+"reveive_analyse "+receive);
		return info_array;
	}
	
	public String[] ErrorReturn(Server_in object_in,int clientid){
		String[] info_array= new String[2];
		String tempname="temptory_user"+Integer.toString(clientid);
		OutputStream out= object_in.get_out();
		TCPEchoServer.set_map_out(tempname, out);
		Socket clntSock= object_in.get_clntSock();
		TCPEchoServer.set_map_socket(tempname, clntSock);
		String error= "$$MSSG&&NULL&&THIS USER NOT EXISTED OR PASSWORD WRONG";
		System.out.println("RW_myQ::"+error);
		info_array[0]=tempname;
		info_array[1]=error;
		return info_array;
	}
	
	public boolean put(int clientid, String receive) {
		boolean status=true;
		try {
			String[] info_array= reveive_analyse(clientid, receive);
			rwl.writeLock().lock();
			Server_out.get_myQ().add(info_array);
			if (info_array[1].equals("$$MSSG&&END_FROM_CLIENT")) status=false;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			rwl.writeLock().unlock();
		}
		return status;
	}
	
	public String[] get() {
		String[] info_array={};
		Queue<String[]> myQ= Server_out.get_myQ();
		try {
			rwl.writeLock().lock();
	 		if (!myQ.isEmpty()) {
	 			info_array= myQ.peek();
	 			myQ.remove();
	 		}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			rwl.writeLock().unlock();
		}
		return info_array;
	}
}